#include "stdafx.h"
#include "ctl_chat_view.h"
#include "window_project.h"
#include "config.h"
#include "string_manager.h"

const chat_view g_default_chat_view;

bool chat_view::load(IXMLDOMNode* aXmlNode)
{
	if (!__super::load(aXmlNode))
	{
		return false;
	}

	return true;
}

bool chat_view::save(IXMLDOMNode* aXmlNode)
{
	IXMLDOMElement*		pXmlElement = (IXMLDOMElement*) aXmlNode;
	if (m_strSaveText != g_default_chat_view.m_strSaveText&& 
		(!m_pReference || !m_pReference->exist_property(L"text") || 
		m_pReference->get_property(L"text") != m_strSaveText))
	{
		pXmlElement->setAttribute(L"text",		(_variant_t)m_strSaveText.c_str());
	}
	return true;
}

void chat_view::render(Graphics & aGraphics)
{
	point abPt = get_absolute_pos();
	
	if (m_strText.length() > 0)
	{
		SolidBrush sb(m_clrTextColor);		
		Gdiplus::Font* font = font_manager::get_font(L"face", 1);		
		rect rt = get_absolute_rect();		
		aGraphics.DrawString(m_strText.c_str(), m_strText.length(), 
			font, rt, NULL, &sb);
	}
}

void chat_view::set_image_file(wstring aStrFileName)
{
	m_strImageString = aStrFileName;

	if (m_image)
	{
		delete m_image;
	}

	int spPos = aStrFileName.find_first_of(L'|');

	m_strImgFileName = aStrFileName;
	wstring rectStr  = aStrFileName;
	if (wstring::npos != spPos)
	{
		m_strImgFileName = m_strImgFileName.erase(spPos);
		rectStr = rectStr.erase(0, spPos + 1);	
		load_helper::parse_rect(rectStr, m_imageRect);
	}

	wstring strTmpPath = path_helper::process_file(m_strImgFileName, m_belongTo->get_file_name());
	m_image = new Gdiplus::Image(strTmpPath.c_str(), FALSE);
	m_strImgFileName = strTmpPath;
}

void chat_view::add_attribute(wstring aStrName, wstring aStrValue)
{

	if (aStrName.compare(L"text") == 0)
	{
		m_strSaveText = aStrValue;

		m_strText = string_manager::m_s_sm.get_string(aStrValue);
		return;
	}

	__super::add_attribute(aStrName, aStrValue);
}

void chat_view::build_properties()
{
	__super::build_properties();

	CMFCPropertyGridProperty* group = NULL;
	CMFCPropertyGridProperty* property = NULL;
	group = m_Properties.alloc_group(this, L"chat_view");

	property = m_Properties.alloc_property_normal(this, L"text", (_variant_t)m_strText.c_str());
	property->SetValue((_variant_t)(wchar_t*)m_strText.c_str());
	group->AddSubItem(property);


	m_Properties.add_property(group);
}

s_properties& chat_view::get_properties()
{
	__super::get_properties();

	CMFCPropertyGridProperty* prop = NULL;
	prop = m_Properties.find_by_name(L"text");
	if (prop)
	{
		prop->SetValue((_variant_t)m_strText.c_str());
	}

	return m_Properties;
}

void chat_view::on_property_changed(CMFCPropertyGridProperty* aProperty)
{
	wstring strOldValue;
	wstring strNewValue;

	const wchar_t* strName = aProperty->GetName();
	if (wcscmp(strName, L"text") == 0)
	{
		strOldValue = m_strText;
		m_strText = aProperty->GetValue().bstrVal;
		strNewValue = m_strText;
	}else
	{
		__super::on_property_changed(aProperty);
		return;
	}

	op_modify_ctl* op = new op_modify_ctl(this, wstring((wchar_t*)strName), strOldValue, strNewValue);
	if (m_belongTo)
	{
		m_belongTo->m_undoRedo.operation(op);
	}else
		delete op;

	if (m_belongTo)
	{
		m_belongTo->refrash();
	}
}